<!--
 * @Author: ChunLai
 * @Date: 2022-03-29 11:10:28
 * @LastEditTime: 2025-08-12 17:47:30
 * @Description: 预试算--年龄选择
 * @FilePath: \02.bldinsure\src\views\count\components\SelectAge.vue
-->
<template>
  <div class="pre_count-select-age">
    <div class="select-age-box" @click="timeShow = true">
      <span class="age-box" v-if="objData.birthDate">
        {{ objData.birthDate }}
        {{ objData.birthDate === "" ? "" : `( ${objData.age}周岁 )` }}
      </span>
      <span style="color: #999" v-else>请选择出生日期</span>
      <span class="ms-row-icon" style="margin-left: 5px;"></span>
    </div>

    <!-- 时间选择器弹窗 -->
    <van-popup v-model="timeShow" position="bottom" :style="{ height: '40%' }">
      <van-datetime-picker
        v-model="currentDate"
        type="date"
        :max-date="maxDate"
        :min-date="minDate"
        @cancel="timeShow = false"
        @confirm="handleConfirm"
      />
    </van-popup>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import { dateToString } from "@/methods/date.js";
export default {
  name: "SelectAge",
  props: {
    objData: {
      type: Object,
      default: () => {},
    },
    minDate: {
      type: Date,
      require: true,
    },
    maxDate: {
      type: Date,
      require: true,
    },
    objType: {
      type: String,
      default: () => "bb",
    },
    objIndex: {
      type: Number,
      default: () => 0,
    },
  },
  data() {
    return {
      timeShow: false,
      currentDate: new Date(),
    };
  },
  computed: {
    ...mapGetters({
      mainInsu_e: "risks/mainInsu",
    }),
    hasSexCheckAge() {
      let res = null;
      const mainObj = this.mainInsu_e[0],
        ruleCfg = mainObj.rules || [],
        ruleObj = ruleCfg.find((ele) => ele.type == "sexLimitMaxAge");
      if (ruleObj) {
        res = ruleObj;
      }
      return res;
    },
  },
  methods: {
    handleConfirm(value) {
      let obj = {
        birthDate: dateToString(value),
        age: this.$GlobalFuns.userAge(dateToString(value)),
      };
      // 主险性别限制年龄校验规则(长城/00369)
      if (this.objType == "bb" && this.hasSexCheckAge) {
        const usex = objData.sex,
          cfgObj = this.hasSexCheckAge.config.find((ele) => ele.sex == usex);
        if (cfgObj) {
          let errTip = "",
            errStr = `被保险人为${usex == "1" ? "女性" : "男性"}`;
          if (cfgObj.max && obj.age > cfgObj.max) {
            errTip = `${errStr}最大投保年龄为${cfgObj.max}岁`;
          }
          console.log(cfgObj.min);
          if (cfgObj.min && obj.age < cfgObj.min) {
            errTip = `${errStr}最小投保年龄为${cfgObj.min}岁`;
          }
          if (errTip) {
            return this.$toast(errTip);
          }
        }
      }
      this.timeShow = false;
      this.$emit("showTimes", obj, this.objType, this.objIndex);
    },
  },
};
</script>

<style lang="less" scoped>
.select-age-box {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  line-height: 1;
  font-size: 15px;

  .age-box {
    color: var(--black-3);
  }
}

</style>
